﻿namespace PugLib.Threading
{
    using System;

    // Notices: Written by Jeffrey Richter
    public class AsyncResult<TResult> : AsyncResultNoResult
    {
        // Field set when operation completes
        private TResult _result;

        public AsyncResult(AsyncCallback callback, Object state)
            : base(callback, state)
        {
        }

        public void SetAsCompleted(TResult result, Boolean completedSynchronously)
        {
            // Save the asynchronous operation's result
            _result = result;

            // Tell the base class that the operation completed sucessfully (no exception)
            SetAsCompleted(null, completedSynchronously);
        }

        public new TResult EndInvoke()
        {
            base.EndInvoke(); // Wait until operation has completed 
            return _result; // Return the result (if above didn't throw)
        }
    }
}